package ee.ut.math.tvt.mitupead.ui.tabs;

import ee.ut.math.tvt.mitupead.domain.data.StockItem;
import ee.ut.math.tvt.mitupead.ui.model.SalesSystemModel;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.NoSuchElementException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;


public class StockTab {

  private JButton addItem;

  private SalesSystemModel model;

  public StockTab(SalesSystemModel model) {
    this.model = model;
  }

  // warehouse stock tab - consists of a menu and a table
  public Component draw() {
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    GridBagLayout gb = new GridBagLayout();
    GridBagConstraints gc = new GridBagConstraints();
    panel.setLayout(gb);

    gc.fill = GridBagConstraints.HORIZONTAL;
    gc.anchor = GridBagConstraints.NORTH;
    gc.gridwidth = GridBagConstraints.REMAINDER;
    gc.weightx = 1.0d;
    gc.weighty = 0d;

    panel.add(drawStockMenuPane(), gc);

    gc.weighty = 1.0;
    gc.fill = GridBagConstraints.BOTH;
    panel.add(drawStockMainPane(), gc);
    return panel;
  }

  // warehouse menu
  private Component drawStockMenuPane() {
    JPanel panel = new JPanel();

    GridBagConstraints gc = new GridBagConstraints();
    GridBagLayout gb = new GridBagLayout();

    panel.setLayout(gb);

    gc.anchor = GridBagConstraints.NORTHWEST;
    gc.weightx = 0;

    addItem = new JButton("Add");
    addItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        lisaUusToode();
      }});
    gc.gridwidth = GridBagConstraints.RELATIVE;
    gc.weightx = 1.0;
    panel.add(addItem, gc);

    panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    return panel;
  }


  // table of the wareshouse stock
  private Component drawStockMainPane() {
    JPanel panel = new JPanel();

    JTable table = new JTable(model.getWarehouseTableModel());

    JTableHeader header = table.getTableHeader();
    header.setReorderingAllowed(false);

    JScrollPane scrollPane = new JScrollPane(table);

    GridBagConstraints gc = new GridBagConstraints();
    GridBagLayout gb = new GridBagLayout();
    gc.fill = GridBagConstraints.BOTH;
    gc.weightx = 1.0;
    gc.weighty = 1.0;

    panel.setLayout(gb);
    panel.add(scrollPane, gc);

    panel.setBorder(BorderFactory.createTitledBorder("Warehouse status"));
    return panel;
  }
  
  protected long inputID(){
	  try{
	  	  long tooteID = Long.parseLong(JOptionPane.showInputDialog(null,
	  			  "Sisesta toote ID", JOptionPane.QUESTION_MESSAGE), 10);
	  	  if( !(tooteID <= 0) ){
	  		JOptionPane.showMessageDialog(null ,"Sisesta korrektne ID",
	      			"Error", JOptionPane.ERROR_MESSAGE);
	  		  return inputID();
	  	  } else {
	  		  return tooteID;
	  	  }
	  } catch(NumberFormatException e) {
		  return 0;
	  }

  }
  
  protected String inputName(){
		  String tooteNimi = JOptionPane.showInputDialog(null,
	  			  "Sisesta toote nimi", JOptionPane.QUESTION_MESSAGE);
		  if (!(tooteNimi == null)){
			  JOptionPane.showMessageDialog(null ,"Sisesta korrektne tootenimi",
		      		"Error", JOptionPane.ERROR_MESSAGE);
		  	  return inputName();
		  } else{
			  return tooteNimi;
		  }		  	  
  }
  
  protected int inputQuantity(){
	  int tooteHulk = Integer.parseInt(JOptionPane.showInputDialog(null,
  			  "Sisesta toote hulk", JOptionPane.QUESTION_MESSAGE));
	  if( !(tooteHulk <= 0) ){
	  		JOptionPane.showMessageDialog(null ,"Sisesta korrektne hulk",
	      			"Error", JOptionPane.ERROR_MESSAGE);
	  		  return inputQuantity();
	  	  } else {
	  		  return tooteHulk;
	  	  }
  }
  
  protected double inputPrice(){
	  int tooteHind = Integer.parseInt(JOptionPane.showInputDialog(null,
  			  "Sisesta toote hind", JOptionPane.QUESTION_MESSAGE));
	  if( !(tooteHind <= 0) ){
	  		JOptionPane.showMessageDialog(null ,"Sisesta korrektne hind",
	      			"Error", JOptionPane.ERROR_MESSAGE);
	  		  return inputPrice();
	  	  } else {
	  		  return tooteHind;
	  	  }
  }
  
  protected void lisaUusToode(){
	  long tooteID = inputID();
	  
	  try {
		  StockItem toode = (StockItem)model.getWarehouseTableModel().getItemById(tooteID).clone();
		  int tooteHulk = inputQuantity();
		  toode.setQuantity(tooteHulk);
		  model.getWarehouseTableModel().addItem(toode);
		  
	  } catch (NoSuchElementException e){
		  String tooteNimi = inputName();
		  
		  try {
			  StockItem toode = (StockItem)model.getWarehouseTableModel().getItemByName(tooteNimi).clone();
			  int tooteHulk = inputQuantity();
			  toode.setQuantity(tooteHulk);
			  model.getWarehouseTableModel().addItem(toode);
			  
		  } catch (NoSuchElementException e2){
			  int tooteHulk = inputQuantity();
			  double tooteHind = inputPrice();
			  StockItem	toode = new StockItem(tooteID, tooteNimi, tooteHind, tooteHulk);	
			  model.getWarehouseTableModel().addItem(toode);
			  
		  }
	  }
	  
	  
  }

}
